package com.cskaoyan.service;

import com.cskaoyan.bean.Keyword;
import com.cskaoyan.bean.KeywordExample;
import com.cskaoyan.bo.KeywordUpdateBO;
import com.cskaoyan.mapper.KeywordMapper;
import com.cskaoyan.vo.KeyVO;
import com.cskaoyan.vo.KeywordListVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class KeyServiceImpl implements KeyService{

    @Autowired
    KeywordMapper keywordMapper;
    @Override
    public KeyVO queryInfo(Integer page, Integer limit, String keyword, String url, String sort, String order) {
        PageHelper.startPage(page, limit);

        KeywordExample keywordExample = new KeywordExample();
        keywordExample.setOrderByClause(sort+" "+order);

        KeywordExample.Criteria criteria = keywordExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        if(keyword!=null&&!"".equals(keyword)){
            criteria.andKeywordLike("%" + keyword + "%");
        }

        if(url!=null&&!"".equals(url)){
            criteria.andUrlLike("%" + url + "%");
        }
        List<Keyword> keywords = keywordMapper.selectByExample(keywordExample);

        PageInfo<Keyword> keywordPageInfo = new PageInfo<>();

        long total = keywordPageInfo.getTotal();

        KeyVO keyVO = new KeyVO();

        keyVO.setTotal(total);
        keyVO.setItems(keywords);
        return keyVO;
    }

    @Override
    public KeywordListVO.ItemsBean create(KeywordListVO.ItemsBean itemsBean) {
//        准备发出的bean
        Keyword keyword =new Keyword();
        keyword.setKeyword(itemsBean.getKeyword());
        keyword.setUrl(itemsBean.getUrl());
        keyword.setIsHot(itemsBean.getIsHot());
        keyword.setIsDefault(itemsBean.getIsDefault());
        Date time = new Date(System.currentTimeMillis());
        keyword.setAddTime(time);
        keyword.setUpdateTime(time);
        int i = keywordMapper.insertSelective(keyword);

        KeywordListVO.ItemsBean bean = new KeywordListVO.ItemsBean();
        bean.setId(keyword.getId());
        bean.setKeyword(keyword.getKeyword());
        bean.setUrl(keyword.getUrl());
        bean.setIsHot(keyword.getIsHot());
        bean.setIsDefault(keyword.getIsDefault());
        bean.setAddTime(keyword.getAddTime().toString());
        bean.setUpdateTime(keyword.getUpdateTime().toString());
        return bean;
    }

    @Override
    public void delete(KeywordUpdateBO keyworddelete) {
        Keyword keyword = new Keyword();
        keyword.setId(keyworddelete.getId());
        keyword.setDeleted(true);
        keywordMapper.updateByPrimaryKeySelective(keyword);
    }

    @Override
    public Keyword update(KeywordUpdateBO keywordUpdate) {
        Keyword keyword = new Keyword();
        keyword.setId(keywordUpdate.getId());
        keyword.setKeyword(keywordUpdate.getKeyword());
        keyword.setUrl(keywordUpdate.getUrl());
        keyword.setIsHot(keywordUpdate.getIsHot());
        keyword.setIsDefault(keywordUpdate.getIsDefault());
        keyword.setSortOrder(keywordUpdate.getSortOrder());

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date=null;
        try {
            date = simpleDateFormat.parse(keywordUpdate.getAddTime());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        keyword.setAddTime(date);
        Date date1=null;
        try {
            date1 = simpleDateFormat.parse(keywordUpdate.getAddTime());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        keyword.setUpdateTime(date1);
        keywordMapper.updateByPrimaryKeySelective(keyword);
        return keyword;
    }

    @Override
    public KeywordListVO list(Integer page, Integer limit, String keyword, String url, String sort, String order) {
        //        用于存放最后结果
        KeywordListVO keywordListVO = new KeywordListVO();

        PageHelper.startPage(page,limit);
        KeywordExample keywordExample = new KeywordExample();
        keywordExample.setOrderByClause(sort+" "+order);

        KeywordExample.Criteria criteria = keywordExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        if (url !=null && !"".equals(url)){
            criteria.andUrlLike("%"+url+"%");
        }
        if (keyword !=null && !"".equals(keyword)){
            criteria.andKeywordLike("%"+keyword+"%");
        }
        List<Keyword> keywords = keywordMapper.selectByExample(keywordExample);
        List<KeywordListVO.ItemsBean> beans = new ArrayList<>();
        for (Keyword key : keywords) {
            KeywordListVO.ItemsBean itemsBean = new KeywordListVO.ItemsBean();
            itemsBean.setId(key.getId());
            itemsBean.setKeyword(key.getKeyword());
            itemsBean.setUrl(key.getUrl());
            itemsBean.setIsHot(key.getIsHot());
            itemsBean.setIsDefault(key.getIsDefault());
            itemsBean.setSortOrder(key.getSortOrder());
            itemsBean.setAddTime(key.getAddTime().toString());
            itemsBean.setUpdateTime(key.getUpdateTime().toString());
            itemsBean.setDeleted(key.getDeleted());
            beans.add(itemsBean);
        }
        PageInfo<KeywordListVO.ItemsBean> itemsBeanPageInfo = new PageInfo<>(beans);
        long total =itemsBeanPageInfo.getTotal();
        keywordListVO.setItems(beans);
        keywordListVO.setTotal(total);

        return keywordListVO;

    }
}
